<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
class WechatController extends Controller
{

    protected $weChat;
    public function __construct()
    {
        $this->weChat = app('wechat');
    }

    /**
     * 处理微信的请求消息
     *
     * @return string
     */
    public function serve()
    {
        $this->weChat->server->setMessageHandler(function($message){
            return "欢迎关注天娃！你的ID".$message->FromUserName;
        });
        return  $this->weChat->server->serve();
    }

    /**
     * 设置菜单
     */
    public function setMenu(){
        $buttons = [
            [
                "type" => "view",
                "name" => "天娃商城",
                "key"  => "SHOP",
                "url"  => "http://www.tw131.com/"
            ],
            [
                "name"       => "天娃钱包",
                "sub_button" => [
                    [
                        "type" => "view",
                        "name" => "新手教程",
                        "url"  => "http://yunfu.a3banks.com/wap.php?m=help&a=index&usid=575999&from=singlemessage&isappinstalled=1"
                    ],
                    [
                        "type" => "view",
                        "name" => "实名认证",
                        "url"  => "http://m.eqxiu.com/s/dTjVC2MN?from=singlemessage&isappinstalled=1"
                    ],
                    [
                        "type" => "view",
                        "name" => "支付宝操作",
                        "key" => "ALIPAY",
                        "url" => "http://m.eqxiu.com/s/HVFcQtIH?from=singlemessage&isappinstalled=1"
                    ],
                    [
                        "type" => "view",
                        "name" => "APP下载",
                        "key" => "DOWN",
                        "url" => "http://twqb.a3banks.com/"
                    ],
                ],
            ],
            [
                "name"       => "关于我们",
                "sub_button" => [
                    [
                        "type" => "view",
                        "name" => "关于我们",
                        "url"  => "http://yunfu.a3banks.com/wap.php?m=help&a=index&usid=575999&from=singlemessage&isappinstalled=1"
                    ],
                    [
                        "type" => "view",
                        "name" => "会员中心",
                        "url"  => "http://www.tw131.com/mobile/user.php"
                    ],
                    [
                        "type" => "view",
                        "name" => "推广中心",
                        "key" => "REBATE",
                        "url" => "http://www.tw131.com/mobile/rebate.php?act=buylink"
                    ]
                ],
            ],
        ];
       $result = $this->weChat->menu->add($buttons);
        return response()->json($result);
    }

    /*删除菜单*/
    public function delMenu(){
        $result = $this->weChat->menu->destroy();
        return response()->json($result);
    }

    /*企业下发金额*/
    public function payMoney(Request $request){
        $openid = $request->input('openid');
        $amount = $request->input('amount');
        $desc = $request->input('desc');
        $merchantPayData = [
            'partner_trade_no' => str_random(16), //随机字符串作为订单号，跟红包和支付一个概念。
            'openid' => $openid, //收款人的openid
            'check_name' => 'NO_CHECK',  //文档中有三种校验实名的方法 NO_CHECK OPTION_CHECK FORCE_CHECK
            're_user_name'=>'默认用户',     //OPTION_CHECK FORCE_CHECK 校验实名的时候必须提交
            'amount' => $amount,  //单位为分
            'desc' =>  $desc,
            'spbill_create_ip' => '192.168.0.1',  //发起交易的IP地址
        ];
        $result = $this->weChat->merchant_pay->send($merchantPayData);
        return response()->json($result);
    }

    public function payRefund(Request $request){
        $result = $this->weChat->payment->refund('2017031054337','2017031054337',0.01);
        return response()->json($result);
    }


}
